package com.qyhl.webtv.module_live.teletext.list;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.constant.LiveConstant;
import com.qyhl.webtv.commonlib.entity.live.LiveRoomBean;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.module_live.R;
import com.qyhl.webtv.module_live.R2;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.umeng.analytics.MobclickAgent;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by qyhl on 2017/12/14.
 * 直播列表
 */

public class LiveListFragment extends BaseFragment implements LivesListContract.LiveListView {
    @BindView(R2.id.title)
    TextView mTitle;
    @BindView(R2.id.live_recyclerView)
    ListView mLiveRecyclerView;
    @BindView(R2.id.live_refresh)
    SmartRefreshLayout mLliveRefresh;
    @BindView(R2.id.lives_loading)
    LoadingLayout mLoading;
    @BindView(R2.id.title_layout)
    RelativeLayout title_layout;

    private View view;
    private LivesListPresenter mPresenter;

    List<LiveRoomBean> mData = new ArrayList<>();
    private CommonAdapter<LiveRoomBean> mAdatapter;

    private String title;

    public void setTitle(String title) {
        this.title = title;
    }


    private String liveId = "0";
    private String liveType = "0";


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return view = inflater.inflate(R.layout.live_fragment_live_list, null);
    }

    @Override
    protected void create() {
        mPresenter = new LivesListPresenter(this);
        init();
    }

    @Override
    public void initImmersionBar() {
        if (!TextUtils.isEmpty(title)) {
//            ImmersionBar.with(this)
//                    .statusBarDarkFont(true)
//                    .titleBar(title_layout)
//                    .init();

            ImmersionBar.with(this)
                    .fitsSystemWindows(true)
                    .statusBarDarkFont(true)
                    .statusBarColor(R.color.white)
                    .init();
        }
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void onFirstVisible() {
        loadData();
    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    public static LiveListFragment newInstance(String title) {
        LiveListFragment fragment = new LiveListFragment();
        fragment.setTitle(title);
        return fragment;
    }



    /**
     * 加载数据
     */
    private void loadData() {
        //下拉刷新
        liveId = "0";
        liveType = "0";
        mPresenter.getLiveList(liveId, liveType);
    }



    private void init() {
        if (TextUtils.isEmpty(title)) {
            title_layout.setVisibility(View.GONE);
        } else {
            mTitle.setText(title);
        }
        mLliveRefresh.setEnableLoadMore(true);
        //刷新中禁止操作
        mLliveRefresh.setDisableContentWhenRefresh(true);
        mLliveRefresh.setRefreshHeader(new MaterialHeader(getContext()));
        mLliveRefresh.setRefreshFooter(new ClassicsFooter(getContext()));
        mLoading.setStatus(LoadingLayout.Loading);
        mLiveRecyclerView.setAdapter(mAdatapter = new CommonAdapter<LiveRoomBean>(getContext(), R.layout.live_item_lives_list, mData) {
            @Override
            protected void convert(ViewHolder holder, LiveRoomBean liveRoomBean, int position) {
                holder.setText(R.id.item_live_title, liveRoomBean.getTitle());
                String hitCount = liveRoomBean.getHitcount();
                TextView scanNum = holder.getView(R.id.item_live_friends);
                if (!StringUtils.isEmpty(hitCount)) {
                    scanNum.setText(hitCount + "人参与");
                } else {
                    scanNum.setText("0人参与");
                }

                ImageView bg = holder.getView(R.id.item_live_logo);
                ImageView tag = holder.getView(R.id.item_live_tagtype);
                ImageView review = holder.getView(R.id.live_status);
                Glide.with(getContext()).load(liveRoomBean.getLogo()).apply(new RequestOptions().error(R.drawable.icon_live_no_img)).into(bg);

                review.setVisibility(View.GONE);
                String type = liveRoomBean.getLivetype();
                if ((LiveConstant.MEDIA.equals(type))) {
                    //电视直播
                    tag.setImageResource(R.drawable.icon_lives_tv);
                } else if (LiveConstant.CAMERA.equals(type)) {
                    //移动直播
                } else if (LiveConstant.INTERACTIVE.equals(type)) {
                    //互动直播
                    tag.setImageResource(R.drawable.icon_lives_hulive);
                } else if (LiveConstant.AUDIO.equals(type)) {
                    //电台直播
                    tag.setImageResource(R.drawable.icon_lives_radio);
                } else if (LiveConstant.PROGRAM.equals(type)) {
                    if ("1".equals(liveRoomBean.getType())) {
                        tag.setImageResource(R.drawable.icon_lives_tv);
                    } else {
                        tag.setImageResource(R.drawable.icon_lives_radio);
                    }
                } else if (LiveConstant.LIVENEWS.equals(type)) {
                    //图文直播
                    tag.setImageResource(R.drawable.icon_lives_media);
                    //状态
                    int status = Integer.parseInt(liveRoomBean.getStatus());
                    switch (status) {
                        case 6:
                            //预告
                            review.setVisibility(View.VISIBLE);
                            review.setImageResource(R.drawable.icon_lives_live);
                            break;
                        case 7:
                            //回放
                            review.setVisibility(View.VISIBLE);
                            review.setImageResource(R.drawable.icon_lives_review);
                            break;
                        case 2:
                            //直播中
                            review.setVisibility(View.VISIBLE);
                            review.setImageResource(R.drawable.icon_lives_live);
                            break;
                        case 3:
                            //结束
                            review.setVisibility(View.VISIBLE);
                            review.setImageResource(R.drawable.icon_lives_end);
                            break;
                        default:
                            break;
                    }
                } else {
                    tag.setImageResource(R.drawable.icon_lives_no);
                }
            }


        });


        mLliveRefresh.setOnRefreshListener(refreshlayout -> {
            liveId = "0";
            liveType = "0";
            mPresenter.getLiveList(liveId, liveType);
        });

        mLoading.setOnReloadListener(v -> {
            mLoading.setReloadButtonText("加载中...");
            liveId = "0";
            liveType = "0";
            mPresenter.getLiveList(liveId, liveType);
        });

        mLliveRefresh.setOnLoadMoreListener(refreshLayout -> mPresenter.getLiveList(liveId, liveType));

        //item 点击事件
        mLiveRecyclerView.setOnItemClickListener((parent, view, position, id) -> {
            LiveRoomBean liveBean = mData.get(position);
            String type = liveBean.getLivetype();

            if ((LiveConstant.MEDIA.equals(type))) {
                //电视直播
                Bundle bundle = new Bundle();
                bundle.putString("id", liveBean.getId());
                RouterManager.router(ARouterPathConstant.LIVE_VIDEO,bundle);
            } else if (LiveConstant.CAMERA.equals(type)) {
                //移动直播
            } else if (LiveConstant.INTERACTIVE.equals(type)) {
                ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.LIVE);
                //互动直播
            } else if (LiveConstant.AUDIO.equals(type)) {
                Bundle bundle = new Bundle();
                bundle.putString("id", liveBean.getId());
                RouterManager.router(ARouterPathConstant.LIVE_RADIO,bundle);
                //电台直播
            } else if (LiveConstant.LIVENEWS.equals(type)) {
                ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.TELETEXTDETAIL);
                //图文直播
                Bundle bundle = new Bundle();
                bundle.putString("id", liveBean.getRoomId()+"");
                RouterManager.router(ARouterPathConstant.TELE_VIDEO_ACTIVITY,bundle);
            } else if (LiveConstant.PROGRAM.equals(type)) {
                ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.TELETEXTDETAIL);
                if ("1".equals(liveBean.getType())) {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", liveBean.getRoomId()+"");
                    RouterManager.router(ARouterPathConstant.LIVE_VIDEO,bundle);
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", liveBean.getRoomId()+"");
                    RouterManager.router(ARouterPathConstant.LIVE_RADIO,bundle);
                }
            } else {

            }

        });

    }

    /**
     * 获取数据失败
     *
     * @param message
     */
    @Override
    public void error(String message, boolean isMore) {
        if (isMore) {
            mLliveRefresh.finishLoadMore();
            showToast(message, 4);
        } else {
            mLliveRefresh.finishRefresh();
            errorLoad(message, mLoading);
        }

    }

    /**
     * 获取成功
     *
     * @param response
     */
    @Override
    public void success(List<LiveRoomBean> response, boolean isMore) {
        mLoading.setStatus(LoadingLayout.Success);
        if (isMore) {
            mLliveRefresh.finishLoadMore();
        } else {
            mLliveRefresh.finishRefresh();
            mData.clear();
        }
        liveId = response.get(response.size() - 1).getRoomId() + "";
        liveType = response.get(response.size() - 1).getLivetype();
        mData.addAll(response);
        mAdatapter.notifyDataSetChanged();

    }

    /**
     * 空数据
     */
    @Override
    public void empty(boolean isMore) {
        if (isMore) {
            mLliveRefresh.finishLoadMore();
        } else {
            mLliveRefresh.finishRefresh();
            emptyLoad(mLoading);
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        //baidu
        //umeng
        MobclickAgent.onPageStart("直播列表");
        MobclickAgent.onResume(getContext());
    }

    @Override
    public void onPause() {
        super.onPause();
        //baidu
        //umeng
        MobclickAgent.onPageEnd("直播列表");
        MobclickAgent.onPause(getContext());
    }

}
